package com.chenwut.studentManagerSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {

    private static final String  ADD_STUDENT = "1";
    private static final String  DELETE_STUDENT = "2";
    private static final String  UPDATE_STUDENT = "3";
    private static final String  SELECT_STUDENT = "4";
    private static final String  EXIT = "5";
    public static void startStudentSystem() {
        ArrayList<Student> list = new ArrayList<>();
        while (true) {
            System.out.println("------------欢迎来到学生管理系统------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            System.out.println("请输入您的选择：");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case ADD_STUDENT -> addStudent(list);
                case DELETE_STUDENT -> deleteStudent(list);
                case UPDATE_STUDENT -> updateStudent(list);
                case SELECT_STUDENT -> selectStudent(list);
                case EXIT -> {
                    System.out.println("退出");
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }

    //添加学生
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        Student stu = new Student();
        System.out.println("请输入学生学号：");
        String id = sc.next();
        while (contains(list, id) >= 0) {
            System.out.println("学号重复，请重新输入学生学号：");
            id = sc.next();
        }
        System.out.println("请输入学生姓名：");
        String name = sc.next();

        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();

        System.out.println("请输入学生地址：");
        String address = sc.next();

        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setAddress(address);

        list.add(stu);
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        System.out.println("请输入要删除的学生学号：");
        Scanner sc = new Scanner(System.in);
        String sid = sc.next();
        int result = contains(list, sid);
        if (result != -1) {
            for (int i = 0; i < list.size(); i++) {
                list.remove(result);
                System.out.println("已删除");
            }
        } else {
            System.out.println("该学号不存在");
            return;
        }

    }

    //修改学生
    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生学号：");
        String id = sc.next();
        int result = contains(list, id);
        if (result >= 0) {

            System.out.println("请输入学生姓名：");
            String newName = sc.next();

            System.out.println("请输入学生年龄：");
            int newAge = sc.nextInt();

            System.out.println("请输入学生地址：");
            String newAddress = sc.next();

            list.get(result).setName(newName);
            list.get(result).setAge(newAge);
            list.get(result).setAddress(newAddress);
            System.out.println("修改成功");
        } else {
            System.out.println("该学号不存在");
        }

    }

    //查询学生
    public static void selectStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前无学生信息，请添加后再查询");
            return;//结束方法
        } else {
            System.out.println("id\t\t姓名\t\t年龄\t\t住址");
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).getId() + "\t\t" + list.get(i).getName() + "\t\t" + list.get(i).getAge()
                        + "\t\t" + list.get(i).getAddress());
            }
        }

    }

    public static int contains(ArrayList<Student> list, String id) {
        String sid;
        for (int i = 0; i < list.size(); i++) {
            sid = list.get(i).getId();
            if (id.equals(sid)) {
                return i;
            }
        }
        return -1;
    }
}
